package decompileDemo;


import java.io.*;


public class MyClassLoader extends ClassLoader {
    private String classDir;

    public MyClassLoader() {

    }

    public MyClassLoader(String classDir) {
        this.classDir = classDir;
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        String myPath = classDir + "\\" + name + ".class";
        try {
            FileInputStream fin = new FileInputStream(myPath);
            ByteArrayOutputStream baout = new ByteArrayOutputStream();
            //解密
            cypher(fin, baout);
            fin.close();
            //转为字节数组
            byte[] byteArray = baout.toByteArray();
            return defineClass(byteArray, 0, byteArray.length);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    //异或加密
    public static void cypher(InputStream in, OutputStream out) throws IOException {
        int b = 0;
        while ((b = in.read()) != -1) {
            out.write(b ^ 0xff);
        }
    }

    public static void main(String[] args) throws ClassNotFoundException, IOException {
        String srcPath = args[0];
        String destDir = args[1];

        String destFileName = srcPath.substring(srcPath.lastIndexOf("\\") + 1);
        String destFilePath = destDir + "\\" + destFileName;
        FileInputStream fin = new FileInputStream(srcPath);
        FileOutputStream fout = new FileOutputStream(destFilePath);

        cypher(fin, fout);

        fin.close();
        fout.close();
    }
}
